import org.ofbiz.base.util.UtilDateTime;
import org.ofbiz.base.util.UtilValidate;
import org.ofbiz.entity.util.EntityUtil;
import org.ofbiz.entity.condition.EntityCondition;
import org.ofbiz.entity.condition.EntityOperator;

allGps = 0;
onlineGps = 0;
offlineGps = 0;

allGpsList = [];
onlineGpsList = [];
offlineGpsList = [];

nowTimestampSubtractMinute = UtilDateTime.adjustTimestamp(nowTimestamp, Calendar.MINUTE, -1);

def getAllProducts(String partyId) {
    allGps = 0;
    productRoleList = delegator.findByAnd("ProductRole", ["partyId" : partyId, roleTypeId : "PRODUCT_OWNER"], null , false);
    productRoleList = EntityUtil.filterByDate(productRoleList);
    productRoleList.each { productRole ->
        allGpsList.add([productId : productRole.productId]);
    }
    allGps = productRoleList.size();
    partyRelationshipList = delegator.findByAnd("PartyRelationship", ["partyIdFrom" : partyId], null, false);
    partyRelationshipList = EntityUtil.filterByDate(partyRelationshipList);
    if (partyRelationshipList) {
        partyRelationshipList.each { partyRelationship ->
            allGps = allGps + getAllProducts(partyRelationship.partyIdTo);
        }
    }
    return allGps;
}

def getOnlineProducts(String partyId) {
    onlineGps = 0;
    productRoleList = delegator.findByAnd("ProductRole", ["partyId" : partyId, roleTypeId : "PRODUCT_OWNER"], null , false);
    productRoleList = EntityUtil.filterByDate(productRoleList);
    productRoleList.each { productRole ->
        andCond = [];
        nowTimestampSubtractMinute = UtilDateTime.adjustTimestamp(nowTimestamp, Calendar.MINUTE, -1)
        andCond.add(EntityCondition.makeCondition("fromDate", EntityOperator.GREATER_THAN, nowTimestampSubtractMinute));
        andCond.add(EntityCondition.makeCondition("productId", EntityOperator.EQUALS, productRole.productId));
        cond = EntityCondition.makeCondition(andCond);
        productGeoPointList = delegator.findList("ProductGeoPoint", cond, null, null, null, false);
        if (productGeoPointList) {
            onlineGpsList.add([productId : productRole.productId]);
            onlineGps++;
        }
    }
    partyRelationshipList = delegator.findByAnd("PartyRelationship", ["partyIdFrom" : partyId], null, false);
    partyRelationshipList = EntityUtil.filterByDate(partyRelationshipList);
    if (partyRelationshipList) {
        partyRelationshipList.each { partyRelationship ->
            onlineGps = onlineGps + getOnlineProducts(partyRelationship.partyIdTo);
        }
    }
    return onlineGps;
}

def getOfflineProducts(String partyId) {
    offlineGps = 0;
    productRoleList = delegator.findByAnd("ProductRole", ["partyId" : partyId, roleTypeId : "PRODUCT_OWNER"], null , false);
    productRoleList = EntityUtil.filterByDate(productRoleList);
    productRoleList.each { productRole ->
        productGeoPointList = delegator.findByAnd("ProductGeoPoint", [productId : productRole.productId], ["-fromDate"], false);
        isPlus = false;
        if (productGeoPointList) {
            productGeoPoint = EntityUtil.getFirst(productGeoPointList);
            if (nowTimestampSubtractMinute.compareTo(productGeoPoint.fromDate) == 1)  {
                isPlus = true;
            }
        } else {
            isPlus = true;
        }
        if (isPlus) {
            offlineGpsList.add([productId : productRole.productId]);
            offlineGps++;
        }
    }
    partyRelationshipList = delegator.findByAnd("PartyRelationship", ["partyIdFrom" : partyId], null, false);
    partyRelationshipList = EntityUtil.filterByDate(partyRelationshipList);
    if (partyRelationshipList) {
        partyRelationshipList.each { partyRelationship ->
            offlineGps = offlineGps + getOfflineProducts(partyRelationship.partyIdTo);
        }
    }
    return offlineGps;
}
allGps = getAllProducts(partyId);
onlineGps = getOnlineProducts(partyId);
offlineGps = getOfflineProducts(partyId);

gpsList = [];
if (tabButtonItem == "Online") {
    gpsList = onlineGpsList;
} else if (tabButtonItem == "Offline") {
    gpsList = offlineGpsList;
} else {
    gpsList = allGpsList;
}

context.allGps = allGps
context.onlineGps = onlineGps;
context.offlineGps = offlineGps;

context.allGpsList = allGpsList
context.onlineGpsList = onlineGpsList;
context.offlineGpsList = offlineGpsList;
context.gpsList = gpsList;

